package com.nova.scenic.projectlibs.util.lifecyclehelper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.nova.scenic.projectlibs.util.debug.MyLog;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 * Created by scenic on 16/5/30.
 */
public abstract class LifeCycleLogHelper extends LifeCycleHelperBase {

    private static final String TAG = "LifeCycleLogHelper";


    public LifeCycleLogHelper(LifeCycleHelper helper) {
        super(helper);
    }

    @Override
    public void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        logI();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        logI();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        logI(MyLog.getInstanceName(newConfig));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        logI();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        logI();

    }

    @Override
    public void onResume() {
        super.onResume();
        logI();

    }

    @Override
    public void onPause() {
        super.onPause();
        logI();

    }

    @Override
    public void onStart() {
        super.onStart();
        logI();

    }

    @Override
    public void onStop() {
        super.onStop();
        logI();

    }

    @Override
    public void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
        super.onApplyThemeResource(theme, resid, first);
        logI("theme " + theme + " resid " + resid + " first " + first);

    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        logI(MyLog.getInstanceName(intent));


    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        logI(MyLog.getInstanceName(savedInstanceState));

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        logI(MyLog.getInstanceName(outState));

    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        logI();

    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        logI();

    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {

        logD("parent " + getViewMsg(parent) + " name " + name + " context " + context + " attrs " + attrs);
        return super.onCreateView(parent, name, context, attrs);
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {

        logD(MyLog.getInstanceName(event));
        return super.onGenericMotionEvent(event);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        logI("level is " + level);


        switch (level) {
            case Activity.TRIM_MEMORY_UI_HIDDEN:
                MyLog.i(TAG, this + " ui hide");
                break;
            case Activity.TRIM_MEMORY_RUNNING_MODERATE:

                break;
            case Activity.TRIM_MEMORY_RUNNING_LOW:

                break;
            case Activity.TRIM_MEMORY_RUNNING_CRITICAL:

                break;
            case Activity.TRIM_MEMORY_BACKGROUND:

                break;
            case Activity.TRIM_MEMORY_MODERATE:

                break;
            case Activity.TRIM_MEMORY_COMPLETE:
                break;


        }
    }

    @Override
    public boolean dispatchGenericMotionEvent(MotionEvent ev) {

        logD(MyLog.getInstanceName(ev));
        return super.dispatchGenericMotionEvent(ev);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {


        logD(MyLog.getInstanceName(ev));
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {


        logD(MyLog.getInstanceName(event));
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onActivityFinish() {
        super.onActivityFinish();
        logI();

    }

    @Override
    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
        super.dump(prefix, fd, writer, args);

        logI("prefix " + prefix + " args " + Arrays.asList(args).toString());
    }

    @Override
    public void onRestart() {
        super.onRestart();
        logI();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        logI(MyLog.getInstanceName(menu));
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        logI(MyLog.getInstanceName(item));
        return super.onOptionsItemSelected(item);
    }

    private void logI() {
        MyLog.i("Log", "execute", getObjectInstance());
    }

    private void logI(String msg) {
        MyLog.i("Log", msg, getObjectInstance());
    }

    private void logD(String msg) {
        MyLog.d("Log", msg, getObjectInstance());
    }

    private String getViewMsg(View view) {
        if (view == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder();
        builder.append(MyLog.getInstanceName(view))
                .append(" viewID:" + view.getId())
                .append(" viewTag:" + view.getTag());

        return builder.toString();
    }


    @Override
    public void onAttach(Context activity) {
        super.onAttach(activity);
        logI(MyLog.getInstanceName(activity));
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        logI(MyLog.getInstanceName(activity));
    }

    @Override
    public void onDetach() {
        super.onDetach();
        logI();
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        logI();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        logI();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        logI();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        logI();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        logI();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        logI();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        logI();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        logI();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyOptionsMenu() {
        super.onDestroyOptionsMenu();
        logI();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        logI();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        logI();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        logI();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        logI();
        super.onStart(intent, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        logI();
        return super.onUnbind(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        logI();
        return super.onBind(intent);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        logI();
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onRebind(Intent intent) {
        logI();
        super.onRebind(intent);
    }

    @Override
    public void onHandleIntent(Intent intent) {
        logI();
        super.onHandleIntent(intent);
    }

    public abstract String getObjectInstance();
//    Object targetObject = new Object();
//    return  "@" + MyLog.getInstanceName(targetObject);


}
